﻿using CMS.Entities;
using CMS.Models;
using CMS.Windows;
using CommonServiceLocator;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using MahApps.Metro.Controls.Dialogs;
using Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace CMS.ViewModel
{
    public class CargoViewModel : ViewModelBase
    {

        public CargoViewModel()
        {
            
        }

        private List<Cargo> cargos = new List<Cargo>();
        /// <summary>
        /// 所有物资
        /// </summary>
        public List<Cargo> Cargos
        {
            get
            {
                return cargos;
            }
            set
            {
                this.cargos = value;
                RaisePropertyChanged();
            }
        }


      

        private List<CargoType> cargoTypes = new List<CargoType>();
        /// <summary>
        /// 物资类型的列表
        /// </summary>
        public List<CargoType> CargoTypes
        {
            get { return cargoTypes; }
            set { cargoTypes = value; RaisePropertyChanged(); }
        }


        private List<UnitType> unitTypes = new List<UnitType>();
        /// <summary>
        /// 单位类型的列表
        /// </summary>
        public List<UnitType> UnitTypes
        {
            get { return unitTypes; }
            set { unitTypes = value; RaisePropertyChanged(); }
        }


        /// <summary>
        /// 打开添加物资对话框
        /// </summary>
        public RelayCommand OpenAddGargoWindowCommand
        {
            get
            {
                return new RelayCommand(() =>
                {
                    var window = new AddCargoWindow();
                    window.ShowDialog();
                    // 刷新数据
                    Cargos = new CargoProvider().Select();
                });
            }
        }

        /// <summary>
        /// 删除物资命令
        /// </summary>
        public RelayCommand<object> DeleteCargoCommand
        {
            get
            {
                return new RelayCommand<object>((arg) =>
                {
                    if (MessageBox.Show("是否删除当前选定的项目？", "是否确认", MessageBoxButton.YesNo, MessageBoxImage.Information) == MessageBoxResult.Yes) {
                    if (!(arg is Cargo model)) return;
                    var count = new CargoProvider().Delete(model);
                    if (count > 0) AppData.Instance.MainWindow.ShowMessageAsync("删除成功", $"成功删除，{model.Name}");
                    // 刷新数据
                    Cargos = new CargoProvider().Select();
                    }

                });
            }
        }
        /// <summary>
        /// 修改物资命令
        /// </summary>
        public RelayCommand<object> EditCargoCommand
        {
            get
            {
                return new RelayCommand<object>((arg) =>
                {
                    if (!(arg is Cargo model)) return;
                    model.TypeId = this.CargoTypes.FirstOrDefault(t=>t.Name == model.TypeName).Id;
                    model.UnitId = this.UnitTypes.FirstOrDefault(t=>t.Name == model.UnitName).Id;
                    var count = new CargoProvider().Update(model);
                    if (count > 0) AppData.Instance.MainWindow.ShowMessageAsync("删除成功", $"成功删除，{model.Name}");
                    // 刷新数据
                    Cargos = new CargoProvider().Select();
                });
            }
        }



        /// <summary>
        /// 打开入库对话框
        /// </summary>
        public RelayCommand<Cargo> OpenInputCargoWindowCommand
        {
            get
            {
                return new RelayCommand<Cargo>((cargo) =>
                {
                    if (cargo == null) return;
                    var inputCargoViewModel = ServiceLocator.Current.GetInstance<InputCargoViewModel>();
                    if (inputCargoViewModel == null) return;
                    inputCargoViewModel.Cargo = cargo;
                    var window = new InputCargoWindow();
                    object value = window.ShowDialog();
                    // 刷新数据
                    Cargos = new CargoProvider().Select();
                    var records = new RecordProvider().Select();
                    Cargos.ForEach(cargoItem =>
                    {
                        var list = records.FindAll(item => item.CargoId == cargoItem.Id);
                        if (list != null)
                        {
                            var inputCount = list.FindAll(item => item.RecordType == true).Sum(item => item.Number);
                            var outputCount = list.FindAll(item => item.RecordType == false).Sum(item => item.Number);
                            var sumCount = inputCount - outputCount;
                            cargoItem.Sum = sumCount;
                        }
                    });
                });
            }
        }

        /// <summary>
        /// 打开出库对话框
        /// </summary>
        public RelayCommand<Cargo> OpenOutputCargoWindowCommand
        {
            get
            {
                return new RelayCommand<Cargo>((cargo) =>
                {
                    if (cargo == null) return;
                    var outputCargoWindow = ServiceLocator.Current.GetInstance<OutputCargoViewModel>();
                    if (outputCargoWindow == null) return;
                    outputCargoWindow.Cargo = cargo;
                    var window = new OutputCargoWindow();
                    object value = window.ShowDialog();
                    // 刷新数据
                    Cargos = new CargoProvider().Select();
                    var records = new RecordProvider().Select();
                    Cargos.ForEach(cargoItem =>
                    {
                        var list = records.FindAll(item => item.CargoId == cargoItem.Id);
                        if (list != null)
                        {
                            var inputCount = list.FindAll(item => item.RecordType == true).Sum(item => item.Number);
                            var outputCount = list.FindAll(item => item.RecordType == false).Sum(item => item.Number);
                            var sumCount = inputCount - outputCount;
                            cargoItem.Sum = sumCount;
                        }
                    });
                });
            }
        }

    }
}
